import React, { PureComponent } from 'react'
import { store } from '../store'
import { addNumberAction } from '../store/actionCreators'

export class home extends PureComponent {
  constructor() {
    super()
    this.state = {
      counter: store.getState().counter
    }
  }

  componentDidMount() {
    // 订阅
    store.subscribe(()=>{
      const state = store.getState()
      this.setState({counter: state.counter})
    })
  }

  addNumber(num) {
    store.dispatch(addNumberAction(num))
  }

  render() {
    const { counter } = this.state
    return (
      <div>
        <h2>这是home counter: {counter}</h2> 
        <button onClick={()=>{this.addNumber(8)}}>+8</button>
      </div>
    )
  }
}

export default home